package com.dl.base.utils

import android.view.View
import java.lang.ref.WeakReference

class ClickProxy(
    val listener: View.OnClickListener, private var intervalTime: Int = 1000
) : View.OnClickListener {
    private var origin: WeakReference<View.OnClickListener>? = null

    init {
        origin = WeakReference(listener)
    }

    private var lastClick: Long = 0
    override fun onClick(v: View) {
        if (System.currentTimeMillis() - lastClick >= intervalTime) {
            origin?.get()?.onClick(v)
            lastClick = System.currentTimeMillis()
        }
    }
}

inline fun View.click(crossinline listener: (View) -> Unit) {
    this.setOnClickListener {
        listener(this)
    }
}